<template>
  <div class="register-container">
    <transition name="el-fade-in">
      <el-form v-show="show" ref="registerForm" :model="registerForm" :rules="registerRules" class="register-form" auto-complete="on" label-position="left">

        <div class="title-container">
          <h3 class="title">注册</h3>
        </div>
        <span style="color:#dc0000;">带有*的为必填项目</span>
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="registerForm.username"
            placeholder="昵称*"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="registerForm.password"
            :type="passwordType"
            placeholder="密码*"
            name="password"
            tabindex="2"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>
        <el-form-item prop="confirmpassword">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="registerForm.confirmpassword"
            :type="passwordType"
            placeholder="确认密码*"
            name="password"
            tabindex="2"
          />
        </el-form-item>
        <el-form-item prop="email">
          <span class="svg-container">
            <svg-icon icon-class="email" />
          </span>
          <el-input
            ref="email"
            v-model="registerForm.email"
            placeholder="输入邮箱"
            name="email"
            type="email"
            tabindex="2"
          />
        </el-form-item>
        <el-form-item prop="telephone">
          <span class="svg-container">
            <svg-icon icon-class="phone" />
          </span>
          <el-input
            ref="telephone"
            v-model="registerForm.telephone"
            placeholder="输入手机号*"
            name="telephone"
            type="tel"
            tabindex="2"
          />
        </el-form-item>
        <el-form-item prop="realname">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="realname"
            v-model="registerForm.realname"
            placeholder="输入姓名*"
            name="realname"
            tabindex="2"
          />
        </el-form-item>
        <el-form-item prop="sex">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-select v-model="registerForm.sex" style="width:93%" placeholder="请选择性别*">
            <el-option
              v-for="item in sexs"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="invitecode">
          <span class="svg-container">
            <svg-icon icon-class="component" />
          </span>
          <el-input
            ref="invitecode"
            v-model="registerForm.invitecode"
            placeholder="输入企业邀请码"
            name="invitecode"
            tabindex="2"
            @keyup.enter.native="handleRegister"
          />
        </el-form-item>
        <el-button style="width:100%" :loading="loading" type="primary" @click.native.prevent="handleRegister">
          注册
        </el-button>
        <el-link type="primary" style="float:right;margin-top:15px;" @click="$router.go(-1)">已有账号？立刻登录</el-link>
      </el-form>

    </transition>
  </div>
</template>

<script>
import { validEmail, validUsername } from '@/utils/validate'
import { verifyCode } from '@/api/user.js'
export default {
  name: 'Register',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('用户名格式不正确'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不得小于六位'))
      } else if (this.registerForm.password !== this.registerForm.confirmpassword) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    const validateEmail = (rule, value, callback) => {
      if (String(value).trim() !== '' && !validEmail(value)) {
        callback(new Error('邮箱格式不正确'))
      } else {
        callback()
      }
    }
    const validateTel = (rule, value, callback) => {
      if (value.length !== 11) {
        callback(new Error('手机号格式不正确'))
      } else {
        callback()
      }
    }
    const validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error('请输入姓名'))
      } else {
        callback()
      }
    }
    const validateSex = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error('请输入性别'))
      } else {
        callback()
      }
    }
    const validateInviteCode = (rule, value, callback) => {
      if (value === '') {
        callback()
      }
      verifyCode(value).then((response) => {
        if (response.msg === 'ok') {
          callback()
        } else if (response.msg === 'empty') {
          callback(new Error('邀请码似乎无效'))
        }
      })
    }
    return {
      show: false,
      registerForm: {
        username: 'asdfgh',
        password: '123456',
        confirmpassword: '123456',
        email: '',
        telephone: '',
        realname: '',
        sex: '',
        invitecode: ''
      },
      registerRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        email: [{ trigger: 'blur', validator: validateEmail }],
        telephone: [{ required: true, trigger: 'blur', validator: validateTel }],
        realname: [{ required: true, trigger: 'blur', validator: validateName }],
        sex: [{ required: true, trigger: 'blur', validator: validateSex }],
        invitecode: [{ trigger: 'blur', validator: validateInviteCode }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      sexs: [{
        value: '1',
        label: '男'
      }, {
        value: '2',
        label: '女'
      }]
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted: function() {
    this.doAnime()
  },
  methods: {
    doAnime: function() {
      this.show = true
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleRegister() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/register', this.registerForm).then(() => {
            this.$alert('注册完成，点击确定返回主页', '注册成功', {
              confirmButtonText: '确定',
              callback: () => {
                this.$router.push({ path: '/login' })
              }
            })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .register-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.register-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.register-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .register-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 80px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
